package com.block.api.controller.externalapi;

import com.block.common.exception.ApiException;
import com.block.common.model.vo.ResultInfoVO;
import com.block.common.util.HttpReturnUtil;
import com.block.db.entity.SourceBaseInfo;
import com.block.source.service.ISourceHandleCheckUserService;
import com.block.source.service.ISourceHandleImportDataService;
import com.block.source.service.ISourceQueryService;
import com.block.test.reqbean.source.CheckUserInfo;
import com.block.test.reqbean.source.ImportData;
import com.block.test.reqbean.source.QueryParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 线索相关接口
 *
 */
@RestController
@RequestMapping("apisource/v1")
@Validated
public class SourceDataController {

    @Autowired
    ISourceHandleCheckUserService sourceHandleCheckUserService;

    @Autowired
    ISourceHandleImportDataService sourceHandleImportDataService;

    @Autowired
    ISourceQueryService sourceQueryService;

    /**
     * 线索撞库
     * 用户全资撞库（手机号md5）
     */
    @RequestMapping(value = "/checkUser", method = {RequestMethod.POST})
    public String checkUser(@RequestAttribute(value = "requestNo") String requestNo,
                              @RequestAttribute(value = "uri") String uri,
                              @RequestAttribute(value = "ip") String ip,
                              @RequestAttribute(value = "sourceBaseInfo") SourceBaseInfo sourceBaseInfo,
                              @RequestAttribute(value = "tranDataBean") CheckUserInfo checkUserInfo) throws ApiException {

        ResultInfoVO resultInfoVO = null;
        try {
            resultInfoVO = sourceHandleCheckUserService.handleCheckUserInfo(sourceBaseInfo, checkUserInfo);
            return HttpReturnUtil.returnJson(uri, requestNo, resultInfoVO);
        }catch (Exception exception){
            resultInfoVO = ResultInfoVO.error("撞库异常");
            return HttpReturnUtil.returnJson(uri, requestNo, resultInfoVO);
        }
    }

    /**
     * 线索进件
     * 敏感数据传输
     */
    @RequestMapping(value = "/importData", method = {RequestMethod.POST})
    public String importData(@RequestAttribute(value = "requestNo") String requestNo,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "ip") String ip,
                             @RequestAttribute(value = "sourceBaseInfo") SourceBaseInfo sourceBaseInfo,
                             @RequestAttribute(value = "tranDataBean") ImportData importData) throws ApiException {

        ResultInfoVO resultInfoVO = null;
        try {
            resultInfoVO = sourceHandleImportDataService.handleImportDataInfo(sourceBaseInfo, importData);
            return HttpReturnUtil.returnJson(uri, requestNo, resultInfoVO);
        }catch (Exception exception){
            resultInfoVO = ResultInfoVO.error("导单异常");
            return HttpReturnUtil.returnJson(uri, requestNo, resultInfoVO);
        }
    }

    /**
     * 线索进件查询
     */
    @RequestMapping(value = "/queryResult", method = {RequestMethod.POST})
    public String queryResult(@RequestAttribute(value = "requestNo") String requestNo,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "ip") String ip,
                             @RequestAttribute(value = "sourceBaseInfo") SourceBaseInfo sourceBaseInfo,
                             @RequestAttribute(value = "tranDataBean") QueryParam queryParam) throws ApiException {

        ResultInfoVO resultInfoVO = null;
        try {
            resultInfoVO = sourceQueryService.querySourceResult(sourceBaseInfo, queryParam);
            return HttpReturnUtil.returnJson(uri, requestNo, resultInfoVO);
        }catch (Exception exception){
            resultInfoVO = ResultInfoVO.error("查询异常");
            return HttpReturnUtil.returnJson(uri, requestNo, resultInfoVO);
        }
    }

}
